import tablib

# 写操作

# 表各列的标题header
headers = ['url','method','expected']
# 需要存到Excel的数据
data_list = [
    ['https://lemon.ke.qq.com','get','成功'],
    ['https://lemon.ke.qq.com','get','成功']
]

data = tablib.Dataset(*data_list, headers=headers,title='test1')
data.append_col([22,211],header='age')
print(data[0])
print(data['url'])
print(data.headers)
data2 = tablib.Dataset(*data_list, headers=headers,title='test2')

# with open('demo01.xls','wb') as f:
#     f.write(data.xls)

# # with open('demo.xlsx', 'wb') as f:
# #     f.write(data.xlsx)
# #
# #
book = tablib.Databook((data, data2))
book.add_sheet(data)
book.add_sheet(data2)
with open('demo_book.xls','wb') as f:
    f.write(book.xls)



# 读操作

# with open('2018released.xlsx','rb') as f:
#     # 接受2个参数,读出来的数据和读取的文件格式
#     res = tablib.import_set(f.read(),'xlsx')
#     print(res)

# with open('demo_book.xls','rb') as f:
#     res = tablib.Dataset().load(f.read(),'xls')
#     print(res)

# 例子
# def api_tester(url,method,expected):
#     print('正在测试{},请求方法{},预期结果{}'.format(url,method,expected))
# with open('demo.xls','rb') as f:
#     res = tablib.import_set(f.read(),'xls')
#     print(type(res))
#     for i in res:
#         api_tester(*i)



#
# with open('2018released.xlsx','rb') as f:
#     res = tablib.import_book(f.read(),'xlsx')
#     print(res)

#
# with open('demo_book.xls','rb') as f:
#     res = tablib.Databook().load(f.read(),'xls')
#     print(res)


# --------------json转Excel----------

# data = tablib.Dataset()
#
# with open('data.json', 'rb') as f:
#     b = f.read()
#     print(b)
#
# data.json = b
# with open('json_to_excel.xls','wb') as f:
#     f.write(data.xls)
